package com.atmilan.quartz.sync.pull.daigo.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.atmilan.quartz.model.entity.daigo.DaigoProduct;
import com.atmilan.quartz.sync.pull.daigo.mapper.DaigoProductMapper;
import com.atmilan.quartz.sync.pull.daigo.service.IDaigoProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hupeng
 * @since 2020-04-27
 */
@Slf4j
@Service
@Transactional
public class DaigoProductServiceImpl extends ServiceImpl<DaigoProductMapper, DaigoProduct> implements IDaigoProductService {
    @Autowired
    DaigoProductMapper daigoProductMapper;

    @Override
    public List<DaigoProduct> getDeleteList(Date startTime) {
        return this.baseMapper.selectList(Wrappers.<DaigoProduct>lambdaQuery().lt(DaigoProduct::getCreateTime, startTime));
    }

    @Override
    public List<String> allGoodsIdList() {
        List<Object> list = daigoProductMapper.selectObjs(new QueryWrapper<DaigoProduct>().select("goods_id"));
        return list == null ? new ArrayList<String>() : list.stream().map(o -> o.toString()).collect(Collectors.toList());
    }

    @Override
    public List<String> findDeleteGoodsIdList(Date startTime) {
        return daigoProductMapper.findDeleteGoodsIds(startTime);
    }

    @Override
    public boolean replaceIntoProduct(DaigoProduct daigoProduct) {
        if (daigoProduct == null || StringUtils.isBlank(daigoProduct.getGoodsId())) {
            log.warn("goodId is null ! product: {}", daigoProduct);
            return false;
        }
        return daigoProductMapper.replaceIntoProduct(daigoProduct) > 0;
    }

    @Override
    public DaigoProduct getByGoodsId(String goodsId) {
        return daigoProductMapper.selectOne(Wrappers.<DaigoProduct>lambdaQuery().eq(DaigoProduct::getGoodsId, goodsId));
    }

    @Override
    public void deleteAll() {
        daigoProductMapper.deleteAll();
    }
    
    @Override
    public void copyAll() {
        daigoProductMapper.deleteProductBack();
        daigoProductMapper.copyProductBack();
    }
    
}
